package com.shanghaionstar.user;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.more.response.ChangePassResponse;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.login.LoginActivity;
import com.shanghaionstar.user.iview.IChangePasswordView;
import com.shanghaionstar.user.presenter.ChangePasswordPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.AuthorUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.weight.CommonInputLayout;
import com.shanghaionstar.weight.CommonProgressButton;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by 7828wc on 16/7/25.
 */
public class ChangePasswordFragment extends BaseFragment implements IChangePasswordView {
    @FindViewById
    private CommonInputLayout et_user_password, et_new_user_password;
    @FindViewById
    private CommonProgressButton    btn_submit;
    @FindViewById
    private TopLayout               top_layout;
    private MoreActivity            moreActivity;
    private ChangePasswordPresenter presenter;
    private boolean isShowOldPassword = false;
    private boolean isShowNewPassword = false;

    @Override
    public int getLayoutId() {
        return R.layout.more_change_password;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btnBackClick(top_layout);
        moreActivity = (MoreActivity) getActivity();
        presenter = new ChangePasswordPresenter(moreActivity.getScheduler());
        presenter.setView(this);

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (presenter.verifyInput(et_user_password, false) && presenter.verifyInput(et_new_user_password, true)) {
                    CommonUtil.hideKeyBoard(moreActivity, et_user_password.getEditValue());
//                    BusinessUtil.sendReportByClickType(moreActivity, FunctionNameFindId.ChangePassword.getStringValue());
                    presenter.setUpdatePassInfo(et_new_user_password.getText(), et_user_password.getText());
                    presenter.start();
                }
            }
        });

        et_user_password.isShowPassword(isShowOldPassword);
        et_user_password.setRightIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et_user_password.isShowPassword(!isShowOldPassword);
                isShowOldPassword = !isShowOldPassword;
            }
        });

        et_new_user_password.isShowPassword(isShowNewPassword);
        et_new_user_password.setRightIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et_new_user_password.isShowPassword(!isShowNewPassword);
                isShowNewPassword = !isShowNewPassword;
            }
        });
    }

    @Override
    public void setLoading(boolean isLoading) {
        btn_submit.showLoading(isLoading);
        et_user_password.setEditEnabled(!isLoading);
        et_new_user_password.setEditEnabled(!isLoading);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(getActivity(), e);
    }

    @Override
    public void verifyResult(View view, int errorResId) {
        if (view instanceof CommonInputLayout) {
            CommonInputLayout inputLayout = (CommonInputLayout) view;
            inputLayout.setEnabled(true);
            if (errorResId == FinalConstants.Register.NO_ERROR) {
                inputLayout.setCheckHintText(null);
            } else if (errorResId == FinalConstants.Register.CHECK_OK) {
                inputLayout.showLoading(false);
                inputLayout.setRightIcon(R.drawable.onstar_joinme_check_ok);
            } else if (errorResId == FinalConstants.Register.CHECK_CANCEl) {
                inputLayout.setRightIcon(-1);
                inputLayout.showLoading(false);
            } else if (errorResId == FinalConstants.Register.SHOW_LOADING) {
                inputLayout.showLoading(true);
            } else {
                inputLayout.setCheck(false);
                inputLayout.setCheckHintText(errorResId);
            }
        }
    }

    @Override
    public String oldPassword() {
        return et_user_password.getText();
    }

    @Override
    public void onReponseSuccess(ChangePassResponse object) {
        AlertDialogUtil.showAlertDialog(moreActivity, R.string.dialog_warning, R.string.update_pass_success, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                AuthorUtil authorUtil = new AuthorUtil();
                authorUtil.loginOut(moreActivity);

                startActivity(new Intent(moreActivity, LoginActivity.class));
                back();
                moreActivity.finish();
            }
        });
    }

    @Override
    public Activity onActivity() {
        return moreActivity;
    }
}